from paste.script.command import Command

import paste
import os
from  urllib2 import urlopen
import re

from cpswarehouse.config.environment import load_environment

from cpswarehouse.model import SchoolDimension
from cpswarehouse.model import meta


address_regex = re.compile("Address\">([a-zA-Z0-9_ ,\.]+)<br/>([a-zA-Z0-9_ ,\.]+)<br/>")

class CpsScrape(Command):
    # Parser configuration
    summary = "Scrape the school addresses from the cps.edu website."
    usage = "<settings_ini>"
    group_name = "cpswarehouse"
    parser = Command.standard_parser(verbose=False)

    def command(self):
        #make sure the config file arg is set
        if(len(self.args) != 1):
            print self.parser.format_help()
            return
        
        #load the environment
        conf_filename = os.path.abspath(self.args[0])
        conf = paste.deploy.appconfig("config:%s" % conf_filename)
        load_environment(conf.global_conf, conf.local_conf)
        
        
        schools = meta.Session.query(SchoolDimension).filter(SchoolDimension.address == None)

        for school in schools:
            url = "http://cps.edu/Schools/Pages/school.aspx?unit=%s" % school.unit
            print url
            lines = urlopen(url).readlines()
            #lines = open('/home/mbone/Developer/cpswarehouse/out').readlines()
            matched_lines = [address_regex.search(line) for line in lines if address_regex.search(line) is not None]
            if(len(matched_lines) == 1):
                match = matched_lines[0]

                street = match.group(1).strip()
                city =  match.group(2).strip()

                school.address = " ".join([street, city])
                
            else:
                print "no match"
            meta.Session.commit()


